﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using CI.Interfaces;
using CI.Utils;

namespace CI.Mesopotamia
{
    /// <summary>
    /// Local XML scenario files to be used
    /// </summary>
    public class ScenarioLocalXML : Scenario
    {
        /// <summary>
        /// Load a scenario
        /// </summary>
        public override void Load()
        {
            #region Argument Checks
            if (string.IsNullOrEmpty(ScenarioName))
                throw new ArgumentException("ScenarioName");
            #endregion

            SimEnv.TraceLogger.LogDebug("XML Scenario Load");
            try
            {
                using (Stream sw = new FileStream(GetScenarioFilePath(), FileMode.Open, FileAccess.Read))
                {
                    // load the scenario
                    var dataContractSerializer = new DataContractSerializer(GetType());
                    var result = (IScenario)dataContractSerializer.ReadObject(sw);

                    // copy the results into our class
                    CopyScenario(result, this);
                }
            }
            catch (Exception ex)
            {
                SimEnv.TraceLogger.LogException("Error Loading Scenario", ex);
                throw;
            }
        }

        private string GetScenarioFilePath()
        {
            return Path.Combine(ResourceFolders.UsersResourceFolder(), ScenarioName + Globals.ScenarioMatch);
        }

        /// <summary>
        /// Save the scenario
        /// </summary>
        public override void Save()
        {
            #region Argument Checks
            if (string.IsNullOrEmpty(ScenarioName))
                throw new ArgumentException("ScenarioName");
            #endregion

            SimEnv.TraceLogger.LogDebug("XML Scenario Save");
            try
            {
                using (Stream sw = new FileStream(GetScenarioFilePath(), FileMode.Create, FileAccess.ReadWrite))
                {
                    var dataContractSerializer = new DataContractSerializer(GetType());
                    dataContractSerializer.WriteObject(sw, this);
                }
            }
            catch (Exception ex)
            {
                SimEnv.TraceLogger.LogException("Error saving Scenario", ex);
            }

            SimEnv.TraceLogger.LogDebug("XML Scenario Save - Finished");
        }

        /// <summary>
        /// get a list of available scenario's
        /// </summary>
        /// <returns></returns>
        public override IEnumerable<string> GetScenariosAvailable()
        {
            // results
            var results = new List<string>();

            // get a list of scenarios in the xml file location
            var files = Directory.GetFiles(ResourceFolders.UsersResourceFolder(), "*" + Globals.ScenarioMatch);

            // if we found files
            if ((files != null) && (files.Length > 0))
            {
                foreach (string filename in files)
                {
                    results.Add(filename.ToLower().Replace(ResourceFolders.UsersResourceFolder() + "\\", "").Replace(Globals.ScenarioMatch, ""));
                }

            }

            return results;
        }

        public override CI.WebService.ScenarioOrganismCollection GetScenarioOrganisms(string scenarioName, int startIndex, int endIndex)
        {
            // todo : implement this
            throw new NotImplementedException();
        }

    }
}